<script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
<div id="app">
  <div class="cart-container">
    <div class="cart-item" v-for="(item, index) in all" :key="index">
      <div class="item-checkbox">
        <input type="checkbox" @click="check(index)" :checked="item.is" />
      </div>
      <div class="item-info">
        <div class="item-name">{{item.name}}</div>
        <div class="item-spec">
          <span class="color-option">黑色</span>
          <span class="storage-option">12GB+256GB</span>
        </div>
        <div class="price-section">
          <span class="current-price">{{item.price | fn()}}</span>
        </div>
      </div>
      <div class="quantity-control">
        <input type="number" v-model="item.num" min="1" />
      </div>
      <div class="item-actions">
        <div class="item-price">{{(item.price * item.num) | fn()}}</div>
        <div class="action-buttons">
          <button class="delete-btn">删除</button>
        </div>
      </div>
    </div>

    <div class="select-all-section">
      <input
        type="checkbox"
        id="selectAll"
        class="select-all-checkbox"
        @click="selectAll()"
        :checked="isAll()"
      />
      <label for="selectAll" class="select-all-label">全选</label>
    </div>

    <div class="cart-summary">
      <div class="selected-count">已选择 {{selectedCount}} 件商品</div>
      <div class="total-price">
        <span>总价：</span>
        <span class="amount" v-if=" !selectedCount">￥0.00</span>
        <span class="amount" v-if=" selectedCount"
          >{{totalPrice-promotionDiscount | fn()}}</span
        >

        <span class="promotion" v-if="promotionDiscount && selectedCount"
          >促销：{{totalPrice | fn()}} -{{promotionDiscount | fn()}}</span
        >
      </div>
    </div>
  </div>
</div>

<script>
  new Vue({
    el: "#app",
    data: {
      all: [
        {
          name: "小米13 保卡光学镜头 第二代镜花8处 理想 超强拉屏幕 120Hz高清 67W快 远眼旁",
          num: 1,
          price: 4599.0,
          is: false,
          gifts: [
            "健康无忧服务季卡",
            "体验卡免费领，多款APP免选，超大流量任性...",
          ],
        },
        {
          name: "Redmi K50 天用8100 2K柔性直屏 OIS光学防抖 67W快充 5500mAh大 远眼旁",
          num: 1,
          price: 2399.0,
          is: false,
          gifts: [
            "小米充电宝 10000mAh 22.5W 随机发货 ...",
            "体验卡免费领，多款APP免选，超大流量任性...",
          ],
        },
        {
          name: "Redmi Note12Pro 5G IMX766 旗舰影像 OIS光学防抖 OLED柔性直屏 远眼旁",
          num: 1,
          price: 1999.0,
          is: false,
          gifts: ["体验卡免费领，多款APP免选，超大流量任性..."],
        },
      ],
      promotionDiscount: 6000,
    },
    methods: {
        isAll(){
            if(this.all.every(item=>item.is == true)){
                return true
            }else{
                return false
            }
        },
      check(index) {
        if (this.all[index].is) {
          this.all[index].is = false;
        } else {
          this.all[index].is = true;
        }
      },
      selectAll() {
        this.all.forEach((item) => {
          item.is = true;
        });
      },
    },
    filters: {
      fn(item) {
        if (item < 0) {
          item = 0;
        }
        return "￥" + item.toFixed(2);
      },
    },
    computed: {
      selectedCount() {
        return this.all.filter((item) => item.is == true).length;
        // return this.all.reduce((total, item) => total + item.num, 0);
      },
      totalPrice() {
        let all2 = this.all.filter((item) => item.is == true);
        return all2.reduce((total, item) => total + item.price * item.num, 0);
      },
    },
  });
</script>

<style>
  select-all-container {
    padding: 10px 15px;
    background: #f5f5f5;
    border-bottom: 1px solid #eee;
    margin-bottom: 10px;
  }

  .select-all-checkbox {
    margin-right: 10px;
    vertical-align: middle;
  }

  .select-all-label {
    font-size: 14px;
    color: #333;
    vertical-align: middle;
    cursor: pointer;
  }

  .cart-container {
    font-family: Arial, sans-serif;
    max-width: 800px;
    margin: 0 auto;
    padding: 10px;
  }

  .cart-item {
    display: flex;
    flex-wrap: wrap;
    border-bottom: 1px solid #eee;
    padding: 15px 0;
    position: relative;
  }

  .item-checkbox {
    width: 30px;
    padding-right: 10px;
  }

  .item-info {
    flex: 1;
    min-width: 200px;
  }

  .item-name {
    font-size: 14px;
    margin-bottom: 5px;
    line-height: 1.4;
  }

  .item-spec {
    margin: 5px 0;
    font-size: 12px;
    color: #666;
  }

  .color-option,
  .storage-option {
    margin-right: 10px;
  }

  .price-section {
    margin: 5px 0;
  }

  .current-price {
    font-size: 18px;
    color: #f20;
    font-weight: bold;
    margin-right: 10px;
  }

  .price-change {
    font-size: 12px;
    color: #999;
    margin-right: 10px;
  }

  .stock-status {
    font-size: 12px;
    color: #39b54a;
  }

  .quantity-control input {
    width: 50px;
    padding: 5px;
    border: 1px solid #ddd;
    text-align: center;
  }

  .item-actions {
    min-width: 120px;
    text-align: right;
  }

  .item-price {
    font-size: 16px;
    color: #f20;
    font-weight: bold;
    margin-bottom: 5px;
  }

  .delete-btn,
  .save-btn {
    background: none;
    border: none;
    color: #666;
    font-size: 12px;
    cursor: pointer;
    padding: 2px 5px;
  }

  .delete-btn:hover,
  .save-btn:hover {
    color: #f20;
  }

  .gifts-section {
    width: 100%;
    margin-top: 10px;
    padding-left: 40px;
  }

  .gift-item {
    font-size: 12px;
    color: #666;
    margin: 3px 0;
  }

  .gift-tag {
    color: #f20;
  }

  .view-price {
    color: #06c;
    margin-left: 5px;
    cursor: pointer;
  }

  .cart-summary {
    text-align: right;
    padding: 15px 0;
    font-size: 14px;
  }

  .selected-count {
    margin-bottom: 5px;
  }

  .total-price {
    font-weight: bold;
  }

  .amount {
    color: #f20;
    font-size: 18px;
  }

  .promotion {
    color: #999;
    margin-left: 10px;
    font-weight: normal;
  }
</style>
